﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Alos.Entity;
using Alos.Common;
using Alos.IRepository;
using Alos.IService;
using PetaPoco;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace Alos.Service
{
    public class ReExamDetailService : BaseService<ReExamDetail>, IReExamDetailService
    {
        IReExamDetailRepository ReExamDetailRepository;

        public ReExamDetailService(IReExamDetailRepository _ReExamDetailRepository)
        {
            ReExamDetailRepository = _ReExamDetailRepository;
        }
        /// <summary>
        /// 添加学生试题答题详情记录表
        /// </summary>
        /// <param name="ReExamDetail">学生试题答题详情记录表</param>
        /// <returns></returns>
        public bool CreatReExamDetail(ReExamDetail ReExamDetail)
        {
            return ReExamDetailRepository.CreatReExamDetail(ReExamDetail);
        }

        public bool Add(List<ReExamDetail> lRed)
        {
            try
            {
                lRed.ForEach(red =>
                {
                    red.AnswerTime = DateTime.Now;
                    red.IsCorrect = red.IsSetCorrect ? red.IsCorrect : IsCorrect(lRed, red);
                });
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            return ReExamDetailRepository.Add(lRed);
        }

        public bool IsCorrect(List<ReExamDetail> lred, ReExamDetail red)
        {
            bool _isCorrect = true;
            string _correctAnswer = DEncrypt.Decrypt(red.CorrectAnswer);
            if (string.IsNullOrWhiteSpace(_correctAnswer))
                return false;
            string[] da, _da;
            switch (red.QuestionTypeCode)
            {
                case "QUE0138":
                case "QUE0139":
                case "QUE0140":
                case "QUE0141":
                case "QUE0142":
                case "QUE0020":
                    var asrs = Regex.Split(red.Answer.Trim(',').ToLower(), "{\\-\\|\\|\\-}", RegexOptions.IgnoreCase);
                    _isCorrect = _correctAnswer.Trim(',').ToLower() == asrs[0];
                    break;
                case "QUE0037":
                    da = _correctAnswer.Trim(',').ToLower().Split(',');
                    _da = red.Answer.Trim(',').ToLower().Split(',');
                    if (da.Length == _da.Length)
                    {
                        foreach (var item in da)
                        {
                            _isCorrect = _isCorrect && _da.Contains(item);
                        }
                    }
                    else
                        _isCorrect = false;
                    break;
                case "QUE0039":
                    _isCorrect = red.Answer.Trim(',') == _correctAnswer.Trim(',');
                    break;
                case "QUE0008":
                case "QUE0143":
                case "QUE0147":
                case "QUE0006":
                case "QUE0043":
                case "QUE0145":
                case "QUE0146":
                    var ctas = _correctAnswer.Split(',');
                    foreach (var cta in ctas)
                    {
                        ReExamDetail _red = lred.Find(i => i.StemID.ToString() == cta);
                        _red.IsCorrect = IsCorrect(lred, _red);
                        _red.IsSetCorrect = true;
                        _isCorrect = _isCorrect && (_red.IsCorrect == null ? false : _red.IsCorrect.Value);
                        red.Answer += "{\"StemID\":\"" + _red.StemID + "\",\"IsRight\":\"" + _red.IsCorrect + "\"}" + ",";
                    }
                    red.Answer = "[" + red.Answer.Trim(',') + "]";
                    break;
                case "QUE0046":
                    var _myAns = JsonConvert.DeserializeObject<dynamic>(red.Answer);
                    if (_myAns != null)
                        _isCorrect = _correctAnswer.Trim(',').ToLower() == _myAns.Answer.ToString().Trim(',').ToLower();
                    else
                        _isCorrect = false;
                    break;
                case "QUE0049":
                    var myAnswer = JsonConvert.DeserializeObject<dynamic>(red.Answer);
                    var ctAnswer = JsonConvert.DeserializeObject<dynamic>(_correctAnswer);

                    if (myAnswer != null && ctAnswer != null && myAnswer.Count == ctAnswer.Count)
                    {
                        Func<dynamic, bool> CheckAnswer = (aser) =>
                        {
                            foreach (var _aser in myAnswer)
                            {
                                if (_aser.TestOptionId == aser.TestOptionId)
                                {
                                    string[] myOpts = _aser.CorrectAnswer.ToString().Split(',');
                                    string[] ctOpts = aser.CorrectAnswer.ToString().Split(',');
                                    if (myOpts != null && ctOpts != null && myOpts.Length == ctOpts.Length)
                                    {
                                        bool _isRight = true;
                                        foreach (var ctOpt in ctOpts)
                                        {
                                            _isRight = _isRight && myOpts.Any(opt => opt == ctOpt);
                                        }
                                        return _isRight;
                                    }
                                }
                            }
                            return false;
                        };
                        foreach (var aser in ctAnswer)
                        {
                            _isCorrect = _isCorrect && CheckAnswer(aser);
                        }
                    }
                    else
                    {
                        _isCorrect = false;
                    }
                    break;
                case "QUE0005":
                    _isCorrect = _correctAnswer.Trim(',').ToLower() == red.Answer.Trim(',').ToLower();
                    break;
                default:
                    _isCorrect = false;
                    break;
            }
            return _isCorrect;
        }
    }
}
